/**
 * 付款计划管理初始化
 */
var PayPlan = {
    id: "PayPlanTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
PayPlan.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '编码', field: 'id', visible: true, align: 'center', valign: 'middle'},
        {title: '权限', field: 'authority', visible: false, align: 'center', valign: 'middle'},
        {title: '月份', field: 'month', visible: true, align: 'center', valign: 'middle'},
        {title: '客户名称', field: 'customerCompanyName', visible: true, align: 'center', valign: 'middle'},
        {title: '供应商名称', field: 'providerName', visible: true, align: 'center', valign: 'middle'},
        {title: '付款金额(万元)', field: 'payAmount', visible: true, align: 'center', valign: 'middle'},
        {title: '现金(万元)', field: 'crash', visible: true, align: 'center', valign: 'middle'},
        {title: '银票(万元)', field: 'silverTicket', visible: true, align: 'center', valign: 'middle'},
        {title: '银票期限(月)', field: 'silverTicketPeriod', visible: true, align: 'center', valign: 'middle'},
        {title: '商票(万元)', field: 'businessTicket', visible: true, align: 'center', valign: 'middle'},
        {title: '商票期限(月)', field: 'businessTicketPeriod', visible: true, align: 'center', valign: 'middle'},
        {title: '航信(万元)', field: 'hangxin', visible: true, align: 'center', valign: 'middle'},
        {title: '航信期限(月)', field: 'hangxinPeriod', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
PayPlan.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        PayPlan.seItem = selected[0];
        return true;
    }
};

PayPlan.checkPermission = function(){
    if (this.check()) {
        if (PayPlan.seItem.authority === 1) {
            return true
        }else{
            Feng.info("您没有该操作权限！");
        }
    }
};

/**
 * 点击添加付款计划
 */
PayPlan.openAddPayPlan = function () {
    var index = layer.open({
        type: 2,
        title: '添加付款计划',
        area: ['1000px', '600px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/payPlan/payPlan_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看付款计划详情
 */
PayPlan.openPayPlanDetail = function () {
    if (this.checkPermission()) {
        var index = layer.open({
            type: 2,
            title: '付款计划详情',
            area: ['1000px', '600px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/payPlan/payPlan_update/' + PayPlan.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除付款计划
 */
PayPlan.delete = function () {
    if (this.check()) {
        var operation = function(){
        var ajax = new $ax(Feng.ctxPath + "/payPlan/delete", function (data) {
            Feng.success("删除成功!");
            PayPlan.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("payPlanId", PayPlan.seItem.id);
        ajax.start();
    }
    Feng.confirm("是否删除?", operation);
}
};

PayPlan.resetSearch = function(){
    $("#customerCompanyName").val("");
    PayPlan.search();
}
/**
 * 查询付款计划列表
 */
PayPlan.search = function () {
    var queryData = {};
    queryData['providerName'] = $("#providerName").val();
    PayPlan.table.refresh({query: queryData});
};

/**
 * 导出付款计划excel模板
 */
PayPlan.exportXlsByT = function () {
    window.location.href = Feng.ctxPath + "/payPlan/exportXlsByM";
}

$(function () {
    var defaultColunms = PayPlan.initColumn();
    var table = new BSTable(PayPlan.id, "/payPlan/list", defaultColunms);
    table.setPaginationType("server");
    PayPlan.table = table.init();

    var upload = new $WebUpload({
        uploadId: "upload",
        mimeTypes: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel",
        extensions: "xlsx,xls",
        url: "/payPlan/importExcel",
        multiple:false,
    });
    upload.init();
});
